package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysExamine;
import cn.knightzz.service.SysExamineService;
import cn.knightzz.vo.ExamineCountVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 考核表, 主要记录党员做的好人好事 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-14
 */
@RestController
public class SysExamineController {

    @Resource
    SysExamineService examineService;

    @PostMapping("/examine")
    public JsonResult<Object> addExamine(@RequestBody SysExamine examine) {
        Boolean status = examineService.addExamine(examine);
        if (status) {
            return new JsonResult<>("200", "添加成功!");
        } else {
            return new JsonResult<>("201", "添加失败!");
        }
    }

    @PutMapping("/examine")
    public JsonResult<Object> editExamine(@RequestBody SysExamine examine) {
        Boolean status = examineService.updateExamine(examine);
        if (status) {
            return new JsonResult<>("200", "更新成功!");
        } else {
            return new JsonResult<>("201", "更新失败!");
        }
    }

    @DeleteMapping("/examine/{id}")
    public JsonResult<Object> deleteExamine(@PathVariable Long id) {
        Boolean status = examineService.deleteExamine(id);
        if (status) {
            return new JsonResult<>("200", "删除成功!");
        } else {
            return new JsonResult<>("201", "删除失败!");
        }
    }

    @GetMapping("/examine")
    public JsonResult<HashMap<String, Object>> selectExaminePage(@RequestParam("pageNum") Long pageNum,
                                                                 @RequestParam("pageSize") Long pageSize,
                                                                 @RequestParam("query") String query) {
        IPage<SysExamine> pages = examineService.selectExamine(pageNum, pageSize, query);
        JsonResult<HashMap<String, Object>> result;
        HashMap<String, Object> data = new HashMap<>();
        if (ObjectUtil.isNotNull(pages)) {
            long total = pages.getTotal();
            List<SysExamine> records = pages.getRecords();
            data.put("total", total);
            data.put("examine", records);
            result = new JsonResult<>("200", "查询成功");
        } else {
            data.put("total", 0);
            data.put("examine", new ArrayList<>());
            result = new JsonResult<>("201", "查询失败");
        }
        result.setData(data);
        return result;
    }

    @GetMapping("/examine/count")
    public JsonResult<HashMap<String, Object>> selectExaminePage(@RequestParam("pageNum") Long pageNum,
                                                                 @RequestParam("pageSize") Long pageSize,
                                                                 @RequestParam("userId") Long userId,
                                                                 @RequestParam("query") String query) {
        IPage<ExamineCountVO> pages = examineService.selectCountExamine(pageNum, pageSize, userId, query);
        JsonResult<HashMap<String, Object>> result;
        HashMap<String, Object> data = new HashMap<>();
        if (ObjectUtil.isNotNull(pages)) {
            long total = pages.getTotal();
            List<ExamineCountVO> records = pages.getRecords();
            data.put("total", total);
            data.put("records", records);
            result = new JsonResult<>("200", "查询成功");
            result.setData(data);
            return result;
        } else {

            return new JsonResult<>("201", "查询失败");
        }
    }
}

